package com.batchengine.rssfeed;

import com.batchengine.rss.util.Feed;
import com.batchengine.rss.util.FeedMessage;
import com.batchengine.rss.util.RSSFeedParser;
import com.batchengine.task.DefaultTask;
import com.batchengine.task.TaskContext;
import com.batchengine.task.TaskMode;
import org.apache.log4j.Logger;

import java.util.List;

import static com.batchengine.Definitions.ContextParams.ARGUMENTS;
import static com.batchengine.task.TaskMode.READ_WRITE;
import static com.batchengine.util.LoggingFactory.createLogger;

/**
 * Created by sunil.kalva@mkhoj.com.
 * User: sunilkalva
 * Date: 10 May, 2010
 */
public class RSSTask extends DefaultTask {

    private static final Logger logger = createLogger();

    public RSSTask() {
        super(RSSTask.class.getName());
    }

    protected RSSTask(String name) {
        super(RSSTask.class.getName());
    }

    public void execute(TaskContext context) {

        List<String> works = (List<String>) context.getContextParam(ARGUMENTS);
        for(String s : works) {
            RSSFeedParser parser = new RSSFeedParser(s);
            Feed feed = parser.readFeed();
            List<FeedMessage> feedMessages = feed.getMessages();
            for(FeedMessage feedMessage : feedMessages) {
                System.out.println(feedMessage);
            }
        }
    }

    public TaskMode getTaskMode() {
        return READ_WRITE;
    }
}
